
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Maven中一些技巧 | SL&#39;s Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="。本文记录使用Maven过程中遇到的一些Tip">
<meta name="keywords" content="Maven">
<meta property="og:type" content="article">
<meta property="og:title" content="Maven中一些技巧">
<meta property="og:url" content="http://caltyang.cn/2016/09/01/Maven中一些技巧/index.html">
<meta property="og:site_name" content="SL&#39;s Blog">
<meta property="og:description" content="。本文记录使用Maven过程中遇到的一些Tip">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160901/mvn_compile_err.png">
<meta property="og:updated_time" content="2018-10-23T06:18:37.453Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Maven中一些技巧">
<meta name="twitter:description" content="。本文记录使用Maven过程中遇到的一些Tip">
<meta name="twitter:image" content="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160901/mvn_compile_err.png">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  <!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.min.js"></script><![endif]-->
  
</head>
<body>
<div id="container">
  <div id="wrap">
    <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">SL&#39;s Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" class="search-form">
          <input type="search" name="word" maxlength="20" class="search-form-input" placeholder="Search">
          <input type="submit" value="" class="search-form-submit">
          <input name=tn type=hidden value="bds">
          <input name=cl type=hidden value="3">
          <input name=ct type=hidden value="2097152">
          <input type="hidden" name="si" value="caltyang.cn">
        </form>
      </div>
    </div>
  </div>
</header>
    <div class="outer">
      <section id="main"><article id="post-Maven中一些技巧" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2016/09/01/Maven中一些技巧/" class="article-date">
  <time datetime="2016-09-01T06:28:19.000Z" itemprop="datePublished">2016-09-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Maven中一些技巧
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>。本文记录使用Maven过程中遇到的一些Tip</p>
<a id="more"></a>
<h2 id="0x01-在pom-xml中使用占位符"><a href="#0x01-在pom-xml中使用占位符" class="headerlink" title="0x01 在pom.xml中使用占位符"></a>0x01 在pom.xml中使用占位符</h2><hr>
<p>比如在配置 Spring 时，可能有很多版本相同的配置，可以使用占位符来统一设置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;properties&gt;</span><br><span class="line">    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;</span><br><span class="line">    &lt;spring.version&gt;4.3.2.RELEASE&lt;/spring.version&gt;</span><br><span class="line">&lt;/properties&gt;</span><br><span class="line"></span><br><span class="line">&lt;dependencies&gt;</span><br><span class="line">    &lt;!--Spring --&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">        &lt;groupId&gt;org.springframework&lt;/groupId&gt;</span><br><span class="line">        &lt;artifactId&gt;spring-context&lt;/artifactId&gt;</span><br><span class="line">        &lt;version&gt;$&#123;spring.version&#125;&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">&lt;/dependencies&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="0x02-指定仓库下载"><a href="#0x02-指定仓库下载" class="headerlink" title="0x02 指定仓库下载"></a>0x02 指定仓库下载</h2><hr>
<p>Maven下载依赖默认是去中央仓库下载，速度比较慢，我们可以在pom.xml中配置指定的仓库地址，如公司私服之类，来加速依赖的下载速度<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;repositories&gt;</span><br><span class="line">    &lt;repository&gt;</span><br><span class="line">        &lt;id&gt;mvnrepository&lt;/id&gt;</span><br><span class="line">        &lt;name&gt;mvnrepository&lt;/name&gt;</span><br><span class="line">        &lt;url&gt;http://www.mvnrepository.com/&lt;/url&gt;</span><br><span class="line">        &lt;layout&gt;default&lt;/layout&gt;</span><br><span class="line">        &lt;releases&gt;</span><br><span class="line">            &lt;enabled&gt;true&lt;/enabled&gt;</span><br><span class="line">        &lt;/releases&gt;</span><br><span class="line">        &lt;snapshots&gt;</span><br><span class="line">            &lt;enabled&gt;false&lt;/enabled&gt;</span><br><span class="line">        &lt;/snapshots&gt;</span><br><span class="line">   &lt;/repository&gt;</span><br><span class="line">&lt;/repositories&gt;</span><br></pre></td></tr></table></figure></p>
<p>或者直接在 ~/.m2/settings.xml 中配置全局的<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;settings&gt;    </span><br><span class="line">  ...    </span><br><span class="line">  &lt;profiles&gt;    </span><br><span class="line">    &lt;profile&gt;    </span><br><span class="line">      &lt;id&gt;dev&lt;/id&gt;    </span><br><span class="line">      &lt;!-- repositories and pluginRepositories here--&gt;    </span><br><span class="line">    &lt;/profile&gt;    </span><br><span class="line">  &lt;/profiles&gt;    </span><br><span class="line">  &lt;activeProfiles&gt;    </span><br><span class="line">    &lt;activeProfile&gt;dev&lt;/activeProfile&gt;    </span><br><span class="line">  &lt;/activeProfiles&gt;    </span><br><span class="line">  ...    </span><br><span class="line">&lt;/settings&gt;</span><br></pre></td></tr></table></figure></p>
<p>。参考：<a href="http://blog.csdn.net/dongnan591172113/article/details/7685164" target="_blank" rel="noopener">http://blog.csdn.net/dongnan591172113/article/details/7685164</a></p>
<h2 id="0x03-执行Maven-target时使用不同的配置"><a href="#0x03-执行Maven-target时使用不同的配置" class="headerlink" title="0x03 执行Maven target时使用不同的配置"></a>0x03 执行Maven target时使用不同的配置</h2><hr>
<p>使用 settings 参数<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn --settings ~/.m2/settings-customer1.xml clean install</span><br></pre></td></tr></table></figure></p>
<p>或者用缩写<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn –s ~/.m2/settings-customer1.xml clean install</span><br></pre></td></tr></table></figure></p>
<h2 id="0x04-测试时指定类"><a href="#0x04-测试时指定类" class="headerlink" title="0x04 测试时指定类"></a>0x04 测试时指定类</h2><hr>
<p>使用代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mvn test -Dtest=[ClassName]</span><br><span class="line">mvn test -Dtest=[ClassName]#[MethodName]</span><br></pre></td></tr></table></figure></p>
<p>同时支持通配符<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn test -Dtest=MyClassTest#*test*</span><br></pre></td></tr></table></figure></p>
<p>。参考：<a href="https://www.oschina.net/code/snippet_157514_37277" target="_blank" rel="noopener">https://www.oschina.net/code/snippet_157514_37277</a></p>
<h2 id="0x05-mvn-compile时报错"><a href="#0x05-mvn-compile时报错" class="headerlink" title="0x05 mvn compile时报错"></a>0x05 mvn compile时报错</h2><hr>
<p><img src="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160901/mvn_compile_err.png" alt=""><br>可能是因为compiler的版本和实际使用的版本不同造成，在pom.xml中手工指定版本即可<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;plugin&gt;</span><br><span class="line">    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;</span><br><span class="line">    &lt;configuration&gt;</span><br><span class="line">        &lt;source&gt;1.8&lt;/source&gt;</span><br><span class="line">        &lt;target&gt;1.8&lt;/target&gt;</span><br><span class="line">    &lt;/configuration&gt;</span><br><span class="line">&lt;/plugin&gt;</span><br></pre></td></tr></table></figure></p>
<p>当然，更好是配置一个property，通过占位符配置</p>
<h2 id="0x06-关闭-package-等-phase-时自动test"><a href="#0x06-关闭-package-等-phase-时自动test" class="headerlink" title="0x06 关闭 package 等 phase 时自动test"></a>0x06 关闭 package 等 phase 时自动test</h2><hr>
<p>。在pom.xml中增加配置变量<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;properties&gt;</span><br><span class="line">    &lt;maven.test.skip&gt;true&lt;/maven.test.skip&gt;</span><br><span class="line">&lt;/properties&gt;</span><br></pre></td></tr></table></figure></p>
<p>。或者也可以手工调用的时候传参<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn clean package -Dmaven.test.skip=true</span><br></pre></td></tr></table></figure></p>
<p>。参考：<a href="http://stackoverflow.com/questions/7456006/maven-packaging-without-test-skip-tests" target="_blank" rel="noopener">http://stackoverflow.com/questions/7456006/maven-packaging-without-test-skip-tests</a></p>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://caltyang.cn/2016/09/01/Maven中一些技巧/" data-id="cjnlj8faz0001h9jb8rvbwwtq" class="article-share-link">Share</a>
      

      

      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Maven/">Maven</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/09/01/SpringBoot中关闭httponly/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          SpringBoot中关闭httponly
        
      </div>
    </a>
  
  
    <a href="/2016/04/29/Homebrew是如何工作的/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Homebrew是如何工作的</div>
    </a>
  
</nav>

  
</article>

</section>
      
      <aside id="sidebar">
  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/">Linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Maven/">Maven</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OSX/">OSX</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Spring/">Spring</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/工具/">工具</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/数据库/">数据库</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/架构/">架构</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/网络/">网络</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/设计/">设计</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Java/" style="font-size: 20px;">Java</a> <a href="/tags/Linux/" style="font-size: 10px;">Linux</a> <a href="/tags/Maven/" style="font-size: 10px;">Maven</a> <a href="/tags/OSX/" style="font-size: 13.33px;">OSX</a> <a href="/tags/Spring/" style="font-size: 10px;">Spring</a> <a href="/tags/linux/" style="font-size: 16.67px;">linux</a> <a href="/tags/工具/" style="font-size: 13.33px;">工具</a> <a href="/tags/数据库/" style="font-size: 10px;">数据库</a> <a href="/tags/架构/" style="font-size: 10px;">架构</a> <a href="/tags/网络/" style="font-size: 10px;">网络</a> <a href="/tags/设计/" style="font-size: 10px;">设计</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">June 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">January 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">December 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">March 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">February 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">December 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">October 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">September 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">April 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">March 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">August 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">December 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/09/">September 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/06/">June 2013</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2018/10/23/使用SSH隧道和内网渗透访问家中服务器/">使用SSH隧道和内网渗透访问家中服务器</a>
          </li>
        
          <li>
            <a href="/2018/06/11/单例的几种写法/">单例的几种写法</a>
          </li>
        
          <li>
            <a href="/2018/01/14/记OSX中遇到的一次Finder文件灰化的问题/">记OSX中遇到的一次Finder文件灰化的问题</a>
          </li>
        
          <li>
            <a href="/2017/12/23/记一次解决CLOSE-WAIT/">记一次解决CLOSE_WAIT</a>
          </li>
        
          <li>
            <a href="/2017/12/04/用Nginx进行反向代理/">用Nginx进行反向代理</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
      
    </div>
    <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 Shawn Liu<br>
      Powered by <a href="//hexo.io/" target="_blank">Hexo</a>
      .
      Theme by <a href="https://github.com/xiangming/landscape-plus" target="_blank">Landscape-plus</a>
    </div>
  </div>
</footer>
  </div>
  <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
  <!-- totop start -->
<div id="totop">
<a title="totop"><img src="/img/scrollup.png"/></a>
</div>

<!-- totop end -->


<!-- 百度分享 start -->

<!-- 百度分享 end -->

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>




<script src="/js/script.js"></script>

</div>
</body>
</html>
